package com.xiaoxin.nmzp.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.IdsListUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.NmzpCompany;
import com.ruoyi.system.mapper.NmzpCompanyMapper;
import com.ruoyi.system.service.ISysUserService;
import com.xiaoxin.nmzp.domain.NmzpJobResume;
import com.xiaoxin.nmzp.domain.NmzpResume;
import com.xiaoxin.nmzp.mapper.NmzpJobResumeMapper;
import com.xiaoxin.nmzp.mapper.NmzpResumeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaoxin.nmzp.mapper.NmzpJobMapper;
import com.xiaoxin.nmzp.domain.NmzpJob;
import com.xiaoxin.nmzp.service.INmzpJobService;

/**
 * 职位管理Service业务层处理
 *
 * @author xiaoxin
 * @date 2024-10-18
 */
@Service
public class NmzpJobServiceImpl implements INmzpJobService {
    @Autowired
    private NmzpJobMapper nmzpJobMapper;
    @Autowired
    private NmzpCompanyMapper nmzpCompanyMapper;
    @Autowired
    private ISysUserService syetemUserService;
    @Autowired
    private NmzpResumeMapper nmzpResumeMapper;
    @Autowired
    private NmzpJobResumeMapper nmzpJobResumeMapper;

    /**
     * 查询职位管理
     *
     * @param jobId 职位管理主键
     * @return 职位管理
     */
    @Override
    public NmzpJob selectNmzpJobByJobId(Long jobId) {
        return nmzpJobMapper.selectNmzpJobByJobId(jobId);
    }

    /**
     * 查询职位管理列表
     *
     * @param nmzpJob 职位管理
     * @return 职位管理
     */
    @Override
    public List<NmzpJob> selectNmzpJobList(NmzpJob nmzpJob) {

        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = syetemUserService.selectUserById(userId);
        List<SysRole> roles = sysUser.getRoles();
        boolean flag = false;
        for (SysRole role : roles) {
            if (role.getRoleKey().equals("admin")) {
                flag = true;
                break;
            }
        }
        if (!flag) {//普通用户
            QueryWrapper<NmzpCompany> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            NmzpCompany nmzpCompany = nmzpCompanyMapper.selectOne(queryWrapper);
            if (nmzpCompany == null) {
                return new ArrayList<NmzpJob>();
            }
            nmzpJob.setCompanyId(nmzpCompany.getCompanyId());
        }
        List<NmzpJob> nmzpJobs = nmzpJobMapper.selectNmzpJobList(nmzpJob);
        return nmzpJobs;
    }

    /**
     * 新增职位管理
     *
     * @param nmzpJob 职位管理
     * @return 结果
     */
    @Override
    public int insertNmzpJob(NmzpJob nmzpJob) {
        Long userId = SecurityUtils.getUserId();
        //插入之前查看公司状态
        QueryWrapper<NmzpCompany> nmzpCompanyQueryWrapper = new QueryWrapper<>();
        nmzpCompanyQueryWrapper.eq("user_id", userId);
        NmzpCompany nmzpCompany = nmzpCompanyMapper.selectOne(nmzpCompanyQueryWrapper);
        if (nmzpCompany.getStatus() == 0) {
            return -1;
        }
        Long loginUserCompanyId = getLoginUserCompanyId();
        nmzpJob.setStatus(0L);
        nmzpJob.setPublishUserId(userId);
        nmzpJob.setCompanyId(loginUserCompanyId);
        nmzpJob.setResumeId("");
        if (nmzpJob.getJobBudget() == null || nmzpJob.getJobBudget().equals("")) {
            nmzpJob.setJobBudget("面谈");
        }
        return nmzpJobMapper.insert(nmzpJob);
    }

    /**
     * 修改职位管理
     *
     * @param nmzpJob 职位管理
     * @return 结果
     */
    @Override
    public int updateNmzpJob(NmzpJob nmzpJob) {   //修改后职位状态为待审核
        nmzpJob.setStatus(0L);
        return nmzpJobMapper.updateById(nmzpJob);
        // return nmzpJobMapper.updateNmzpJob(nmzpJob);
    }

    /**
     * 批量删除职位管理
     *
     * @param jobIds 需要删除的职位管理主键
     * @return 结果
     */
    @Override
    public int deleteNmzpJobByJobIds(Long[] jobIds) {
        return nmzpJobMapper.deleteNmzpJobByJobIds(jobIds);
    }

    /**
     * 删除职位管理信息
     *
     * @param jobId 职位管理主键
     * @return 结果
     */
    @Override
    public int deleteNmzpJobByJobId(Long jobId) {
        //根据公司id修改状态
        UpdateWrapper<NmzpJob> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id", jobId);
        updateWrapper.set("status", 2);
        return nmzpJobMapper.update(null, updateWrapper);
        // return nmzpJobMapper.deleteNmzpJobByJobId(jobId);
    }

    /**
     * 根据登录用户获取公司id
     *
     * @return
     */

    public Long getLoginUserCompanyId() {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<NmzpCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        NmzpCompany nmzpCompany = nmzpCompanyMapper.selectOne(queryWrapper);
        return nmzpCompany.getCompanyId();
    }


    /**
     * 职位审核
     *
     * @param nmzpJob
     * @return
     */
    public int examine(NmzpJob nmzpJob) {
        UpdateWrapper<NmzpJob> updateWrapper = new UpdateWrapper<>();
        //根据职位id修改，职位状态
        updateWrapper.eq("job_id", nmzpJob.getJobId());
        updateWrapper.set("status", nmzpJob.getStatus());
        return nmzpJobMapper.update(null, updateWrapper);
    }

    @Override
    public List<NmzpJob> jobList(NmzpJob nmzpJob) {
        QueryWrapper<NmzpJob> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("status", 3);
        List<NmzpJob> nmzpJobs = nmzpJobMapper.selectList(jobQueryWrapper);
        return nmzpJobs;
    }

    /**
     * 职位--搜索管理列表
     *
     * @param nmzpJob 职位管理
     * @return 职位管理集合
     */
    @Override
    public List<NmzpJob> jobListSerch(NmzpJob nmzpJob) {

        QueryWrapper<NmzpJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 3);
        if (nmzpJob.getJobName() != null && !nmzpJob.getJobName().equals("")) {
            queryWrapper.like("job_name", nmzpJob.getJobName());
        }
        //...更多条件
        return nmzpJobMapper.selectList(queryWrapper);
    }

    /**
     * 上/下线
     *
     * @param nmzpJob
     * @return
     */
    public String pop(NmzpJob nmzpJob) {
        Long status = nmzpJob.getStatus();
        NmzpJob nmzpJob1 = nmzpJobMapper.selectById(nmzpJob.getJobId());
        Long status1 = nmzpJob1.getStatus();
        UpdateWrapper<NmzpJob> updateWrapper = new UpdateWrapper<>();
        if (status1 == 0) {
            return "该职位还未审核";
        } else if (status1 == 2) {
            return "该职位未审核通过，请修改后操作";
        } else if (status1 == 1) {//上线操作
            if (status == 3) {
                updateWrapper.set("status", 3);
            }
            if (status == 4) {
                updateWrapper.set("status", 4);
            }
        } else if (status1 == 3) {
            if (status == 3) {
                return "该职位已上线";
            }
            if (status == 4) {
                updateWrapper.set("status", 4);
            }
        } else if (status1 == 4) {
            if (status == 3) {
                updateWrapper.set("status", 3);
            }
            if (status == 4) {
                return "该职位已下线";
            }
        } else {
            return "未知错误";
        }
        updateWrapper.eq("job_id", nmzpJob.getJobId());
        int update = nmzpJobMapper.update(null, updateWrapper);
        if (update > 0) {
            return "操作成功";
        } else {
            return "操作失败";
        }
    }

    /**
     * 简历投递
     *
     * @param nmzpJob
     * @return
     */
    public String deliver(NmzpJob nmzpJob) {
        Long jobId = nmzpJob.getJobId();
        Long resumeId = Long.valueOf(nmzpJob.getResumeId());
        String userId = SecurityUtils.getUserId().toString();
        //是否有简历
        QueryWrapper<NmzpResume> nmzpResumeQueryWrapper = new QueryWrapper<>();
        nmzpResumeQueryWrapper.eq("user_id", userId);
        List<NmzpResume> nmzpResumes = nmzpResumeMapper.selectList(nmzpResumeQueryWrapper);
        if (nmzpResumes.size() < 0) {
            return "请先完善简历";
        }

        QueryWrapper<NmzpJobResume> nmzpJobResumeQueryWrapper = new QueryWrapper<>();
        nmzpJobResumeQueryWrapper.eq("job_id", jobId);
        List<NmzpJobResume> nmzpJobResumes = nmzpJobResumeMapper.selectList(nmzpJobResumeQueryWrapper);
        if (nmzpJobResumes.size() <= 0) {//没有简历投递该岗位
            NmzpJobResume nmzpJobResume = new NmzpJobResume();
            nmzpJobResume.setJobId(jobId);
            //todo 多份简历投递其中一份
            nmzpJobResume.setResumeId(resumeId);
            nmzpJobResume.setStatus("1");
            nmzpJobResumeMapper.insert(nmzpJobResume);
            return "投递成功";
        } else {
            //查看resumeId是否存在list集合中
            boolean flag = false;//默认未投递过
            for (NmzpJobResume nm : nmzpJobResumes) {
                if (nm.getResumeId().equals(nmzpJob.getResumeId())) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return "已经投递过简历";
            } else {
                NmzpJobResume nmzpJobResume = new NmzpJobResume();
                nmzpJobResume.setJobId(jobId);
                nmzpJobResume.setResumeId(resumeId);
                nmzpJobResume.setStatus("1");
                nmzpJobResumeMapper.insert(nmzpJobResume);
                return "投递成功";
            }
        }

    }


    /**
     * 跟据职位id查询，简历集合
     *
     * @param nmzpJob
     * @return
     */
    public List<NmzpResume> jobResumeList(NmzpJob nmzpJob) {
        Long jobId = nmzpJob.getJobId();

        QueryWrapper<NmzpJobResume> nmzpJobResumeQueryWrapper = new QueryWrapper<>();
        nmzpJobResumeQueryWrapper.eq("status", "1");
        nmzpJobResumeQueryWrapper.eq("job_id", jobId);
        List<NmzpJobResume> nmzpJobResumes = nmzpJobResumeMapper.selectList(nmzpJobResumeQueryWrapper);
        List<NmzpResume> nmzpResumeList = new ArrayList<>();
        for (NmzpJobResume nmzpJobResume : nmzpJobResumes) {
            NmzpResume nmzpResume = nmzpResumeMapper.selectById(nmzpJobResume.getResumeId());
            nmzpResumeList.add(nmzpResume);
        }
        for (NmzpResume nm : nmzpResumeList) {
            nm.setUserName(syetemUserService.selectUserById(nm.getUserId()).getUserName());
        }
        return nmzpResumeList;
    }


}
